import {
  Controller,
  ForbiddenException,
  Get,
  HttpException,
  HttpStatus,
  Query,
  UseFilters,
} from '@nestjs/common';
import { AppService } from './app.service';
import { HttpExceptionFilter } from './http-exception.filter';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get('/hello')
  getHello(@Query('username') username: string): string {
    if (username !== 'admin') {
      throw new ForbiddenException('禁止访问', {
        cause: new Error(),
        description: '这里对错误进行描述',
      });
      // throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
    }
    return this.appService.getHello();
  }
  @Get('/sample')
  @UseFilters(new HttpExceptionFilter()) // 使用自定义异常过滤器
  sampleRoute() {
    // 在这里抛出异常
    throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
  }
}
